package com.baizhi.controller;

import com.baizhi.entity.Hero;
import com.baizhi.service.HeroService;
import com.baizhi.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("heroes")
public class HeroController {
    @Autowired
    private HeroService heroService;
    @GetMapping
    public R<PageInfo<Hero>> showHero(Integer pageNum, Integer pageSize){
        PageInfo<Hero> heroPageInfo = heroService.showHeroS(pageNum, pageSize);
        return R.ok(heroPageInfo);
    }
    @PostMapping
    public R addHero(@RequestBody Hero hero){
        try {
            heroService.addHero(hero);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    @DeleteMapping("{id}")
    public R removeHero(@PathVariable("id") Integer heroId){
        try {
            heroService.removeHero(heroId);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    @GetMapping("{id}")
    public R<Hero> showOneHero(@PathVariable("id") Integer heroId){
        Hero hero = heroService.showHeroById(heroId);
        return R.ok(hero);
    }
    @PutMapping
    public R updateHero(@RequestBody Hero hero){
        try {
            heroService.updateHero(hero);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
